package com.test.common.entity.perf;

import com.test.common.enums.perf.PerfReportStatEnum;
import com.test.common.enums.perf.PerfReportTypeEnum;
import lombok.Data;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Data
@Entity
@Table(name = "t_perf_report")
public class PerfReportEntity {

    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public PerfReportEntity() {
    }

    public PerfReportEntity(int taskId, String taskName, int scriptId, String scriptName, int rampTime, int duration, Date setTime, int setQps, int setUsers, String remark, List<PerfAgentEntity> agentEntityList) {
        this();
        this.taskId = taskId;
        this.taskName = taskName;
        this.scriptId = scriptId;
        this.scriptName = scriptName;
        this.rampTime = rampTime;
        this.duration = duration;
        this.setTime = setTime;
        this.status = PerfReportStatEnum.WAITING;
        this.setQps = setQps;
        this.setUsers = setUsers;
        this.remark = remark;
        this.agentEntityList = agentEntityList;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(columnDefinition = "VARCHAR(64) NOT NULL DEFAULT '' COMMENT '脚本（报告）id'")
    private String uuid = UUID.randomUUID().toString();

    @Enumerated(EnumType.STRING)
    private PerfReportTypeEnum reportType = PerfReportTypeEnum.Single;

    @Column(columnDefinition = "INT(9) NOT NULL COMMENT '根据任务ID查询任务名称' ")
    private int taskId;

    @Column(columnDefinition = "VARCHAR(64) NOT NULL DEFAULT ''")
    private String taskName;

    @Column(columnDefinition = "INT(9) NOT NULL COMMENT '根据脚本ID查询脚本名称' ")
    private int scriptId;

    @Column(columnDefinition = "VARCHAR(64) NOT NULL DEFAULT ''")
    private String scriptName;

    @Column(columnDefinition = "INT(11) NOT NULL COMMENT '线程启动时间'")
    private int rampTime;

    @Column(columnDefinition = "INT(11) NOT NULL COMMENT '压测持续时间'")
    private int duration;

    @Column(columnDefinition = "VARCHAR(128) NOT NULL DEFAULT '' COMMENT '报告描述信息，在报告详情里面展示，用作压测报告总结'")
    private String description = "REPLACE ME";

    @Column(columnDefinition = "TEXT NOT NULL COMMENT '报告的备注信息'")
    private String remark = "";

    @Enumerated(EnumType.STRING)
    private PerfReportStatEnum status;

    @Column(columnDefinition = "DATETIME NOT NULL COMMENT '预设的启动时间'")
    private Date setTime = new Date(0);

    @Column(columnDefinition = "INT(11) NOT NULL COMMENT '预设的QPS'")
    private int setQps;

    @Column(columnDefinition = "INT(11) NOT NULL COMMENT '预设的并发值'")
    private int setUsers;

    @Column(columnDefinition = "INT(11) NOT NULL COMMENT '最终QPS'")
    private int qps;

    @Access(value = AccessType.PROPERTY)
    @Column(columnDefinition = "DATETIME NOT NULL COMMENT '实际开始时间'")
    private Date startTime = new Date(0);

    @Access(value = AccessType.PROPERTY)
    @Column(columnDefinition = "DATETIME NOT NULL COMMENT '实际结束时间'")
    private Date endTime = new Date(0);

    @Column(columnDefinition = "DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '任务创建时间'")
    private Date createTime = new Date();

    @Column(columnDefinition = "DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '任务修改时间'")
    private Date modifyTime = new Date();

    @Column(columnDefinition = "INT(4) NOT NULL COMMENT '执行进度，百分比，单位：秒'")
    private int percentAge = 0;

    @Access(value = AccessType.PROPERTY)
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @JoinTable(name = "t_perf_report_agent", joinColumns = {@JoinColumn(name = "report_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "agent_id", referencedColumnName = "id")})
    private List<PerfAgentEntity> agentEntityList;

    @Transient
    private String startTimeStr;

    @Transient
    private String endTimeStr;

    public String getStartTimeStr() {
        if (this.status == PerfReportStatEnum.WAITING) {
            this.startTimeStr = "尚未开始";
        } else {
            this.startTimeStr = format.format(startTime);
        }
        return this.startTimeStr;
    }

    public String getEndTimeStr() {
        if (this.status == PerfReportStatEnum.WAITING) {
            this.endTimeStr = "尚未开始";
        } else if (this.status == PerfReportStatEnum.RUNNING) {
            this.endTimeStr = "尚未结束";
        } else if (this.status == PerfReportStatEnum.STOPPED || this.status == PerfReportStatEnum.FINISHED || this.status == PerfReportStatEnum.ERROR) {
            this.endTimeStr = format.format(endTime);
        }
        return this.endTimeStr;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public static void main(String[] args) {
        System.out.println(format.format(new Date(0)));
    }
}
